package com.third.clock.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.third.clock.MainActivity;
import com.third.clock.R;
import com.third.clock.manager.WindowMessageManager;
import com.third.clock.ui.model.ILeaveActivityView;
import com.third.clock.ui.model.presenter.LeaveActivityPresenter;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/7/26 0026.
 *
 *
 * 请假
 */

public class LeaveActivity extends Activity implements View.OnClickListener,ILeaveActivityView{

    private TextView mTextStartTimeChooes;

    private TextView mTextEndTimeChooes;

    private EditText mEditTextLeave;

    private TimePickerView mStartTimePicker;

    private TimePickerView mEndTimePicker;

    private TextView mTextViewTitle;

    private View mHeadView;

    private Button mButtonLeave;

    private LeaveActivityPresenter mLeaveActivityPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textview_starttime_chooes:
                showStartTimerPicker();
                break;
            case R.id.textview_endtime_chooes:
                showEndTimerPicker();
                break;
            case R.id.btn_leave:
                //mLeaveActivityPresenter.postLeave();
                break;
        }
    }

    private void initView(){
       mHeadView=findViewById(R.id.leave_head_view);
       mTextViewTitle= (TextView) mHeadView.findViewById(R.id.textview_head_title);
       mTextViewTitle.setText("请假");

       mTextStartTimeChooes= (TextView) findViewById(R.id.textview_starttime_chooes);
       mTextEndTimeChooes= (TextView) findViewById(R.id.textview_endtime_chooes);
       mEditTextLeave=(EditText) findViewById(R.id.edittext_leave);
       mButtonLeave= (Button) findViewById(R.id.btn_leave);

       mTextStartTimeChooes.setOnClickListener(this);
       mTextEndTimeChooes.setOnClickListener(this);
       mButtonLeave.setOnClickListener(this);

        mLeaveActivityPresenter=new LeaveActivityPresenter(LeaveActivity.this);
        mLeaveActivityPresenter.setView(this);
   }

   private void showStartTimerPicker(){
       //时间选择器
       mStartTimePicker = new TimePickerBuilder(LeaveActivity.this, new OnTimeSelectListener() {
           @Override
           public void onTimeSelect(Date date, View v) {
               SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               String dateString = formatter.format(date);
               Toast.makeText(LeaveActivity.this, dateString, Toast.LENGTH_SHORT).show();
               mTextStartTimeChooes.setText(dateString);
           }
       })
       .setLabel("年","月","日","时","分","秒")
       .setType(new boolean[]{true, true, true, true, true, true})
        .build();
       mStartTimePicker.show();
   }

    private void showEndTimerPicker(){
        //时间选择器
        mEndTimePicker = new TimePickerBuilder(LeaveActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(date);
                Toast.makeText(LeaveActivity.this, dateString, Toast.LENGTH_SHORT).show();
                mTextEndTimeChooes.setText(dateString);
            }
        })
                .setLabel("年","月","日","时","分","秒")
                .setType(new boolean[]{true, true, true, true, true, true})
                .build();
        mEndTimePicker.show();
    }

    @Override
    public void onFail(String message) {
        if(!TextUtils.isEmpty(message)&&LeaveActivity.this!=null&&!LeaveActivity.this.isFinishing()){
            WindowMessageManager.getInstance().showInfoWithStatus(LeaveActivity.this,message);
        }
    }

    @Override
    public void onSuccess() {
        if(LeaveActivity.this!=null&&!LeaveActivity.this.isFinishing()) {
            WindowMessageManager.getInstance().showSuccessWithStatus(LeaveActivity.this,"提交成功");
        }
    }
}
